import Cookies from 'js-cookie'
// import store from '@/store/index'

const hrefUrl = window.location.href.split('/')[2].split(':')[1]
// set sessionx
const SessionIdKey = 'system_key' + (hrefUrl || 443)
const AuthSessionIdKey = 'auth_system_key' + (hrefUrl || 443)
const UserInfoKey = 'userInfo' + (hrefUrl || 443)
const RoleListKey = 'roleList' + (hrefUrl || 443)
const OemKey = 'Oem' + (hrefUrl || 443)
const AuthUserInfoKey = 'authUserInfo' + (hrefUrl || 443)

export function getSessionId () {
  // return store.getters.sessionId
  return Cookies.get(SessionIdKey)
}
export function setSessionId (sessionId) {
  // sessionStorage.setItem(SessionIdKey, sessionId)
  return Cookies.set(SessionIdKey, sessionId)
}
export function removeSessionId () {
  // sessionStorage.removeItem(SessionIdKey)
  return Cookies.remove(SessionIdKey)
}

// userInfo
export function getUserInfo () {
  // var userInfo = sessionStorage.getItem('userInfo')
  var userInfo = Cookies.get(UserInfoKey)
  if (userInfo != null) {
    try {
      userInfo = JSON.parse(userInfo)
      return userInfo
    } catch (error) {
      return null
    }
  } else {
    return null
  }
}
export function setUserInfo (userInfo) {
  // sessionStorage.setItem('userInfo', JSON.stringify(userInfo))
  Cookies.set(UserInfoKey, JSON.stringify(userInfo))
}
export function removeUserInfo () {
  Cookies.remove(UserInfoKey)
}

// 路由列表
export function getRoleList () {
  var roleList = Cookies.get(RoleListKey)
  if (roleList) {
    return JSON.parse(roleList)
  } else {
    return []
  }
}
export function setRoleList (roleList) {
  Cookies.set(RoleListKey, JSON.stringify(roleList))
}
export function removeRoleList () {
  return Cookies.remove(RoleListKey)
}

// oem标识
export function getOem () {
  var Oem = Cookies.get(OemKey)
  if (Oem) {
    return JSON.parse(Oem)
  } else {
    return ''
  }
}
export function setOem (Oem) {
  Cookies.set(OemKey, JSON.stringify(Oem))
}
export function removeOem () {
  return Cookies.remove(OemKey)
}

// 门户认证session id
export function getAuthSessionId () {
  return Cookies.get(AuthSessionIdKey)
}
export function setAuthSessionId (sessionId) {
  return Cookies.set(AuthSessionIdKey, sessionId)
}
export function removeAuthSessionId () {
  return Cookies.remove(AuthSessionIdKey)
}

// auth userInfo
export function getAuthUserInfo () {
  // var userInfo = sessionStorage.getItem('userInfo')
  var userInfo = Cookies.get(AuthUserInfoKey)
  if (userInfo != null) {
    try {
      userInfo = JSON.parse(userInfo)
      return userInfo
    } catch (error) {
      return null
    }
  } else {
    return null
  }
}
export function setAuthUserInfo (userInfo) {
  // sessionStorage.setItem('userInfo', JSON.stringify(userInfo))
  Cookies.set(AuthUserInfoKey, JSON.stringify(userInfo))
}
export function removeAuthUserInfo () {
  Cookies.remove(AuthUserInfoKey)
}

export function blobToObj (data) {
  return new Promise((resolve, reject) => {
    let reader = new FileReader()
    reader.readAsText(data, 'utf-8')
    reader.onload = function () {
      try {
        resolve(JSON.parse(reader.result))
      } catch (error) {
        resolve()
      }
    }
  })
}
